package demo.tellurium.framework

import org.apache.commons.lang.math.RandomUtils
import org.telluriumsource.ui.widget.dojo.DojoWidget

import demo.util.BaseUtils
import demo.util.Console

/**
 * Base Tellurium Widget object for the Dojo widget
 *
 * @author Roger Xu (roger.xu.2007@gmail.com)
 *
 * Date: Apr 14, 2010
 * 
 */
abstract class BaseDojoWidget extends DojoWidget {

    protected static Console console = BaseUtils.getConsole();

    protected static final int RANDOM_INDEX = -1;

    abstract public String getWidgetName();

    /**
     * if optionSize=5, then range=[1, 5]
     * @param optionSize
     * @return
     */
    protected int getRandomIndex(int optionSize) {
        int index = 0;

        if (optionSize > 0) {
            int max = optionSize;
            index = RandomUtils.nextInt(max) + 1;
        }

        return index;
    }

    protected void clickListOption(String listUid, int index) {
        clickListOption(listUid, null, index);
    }

    protected void clickListOption(String listUid, String optionItemUid, int index) {
        int listSize = getListSize(listUid);

        if (index == RANDOM_INDEX) {
            index = getRandomIndex(listSize);
        }

        console.debug(listUid, index, "/", listSize);
        def optionUid = String.format("%s[%d]", listUid, index);
        if (optionItemUid) {
            optionUid = String.format("%s.%s", optionUid, optionItemUid);
        }

        mouseOver optionUid
        pause 1000

        click optionUid
        pause 500
    }
}